<template>
    <div class="teaching w-100 h-100">
        <el-card class="box-card-table h-100" shadow="never">
            <div slot="header" class="clearfix">
                <span class="fl">添加教学计划</span>
                <el-button class="fr" @click="goback" style="padding:6px;">返回</el-button>
            </div>
            <div class="h-100">
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="计划名称：" prop="bPlanName">
                        <el-input v-model="ruleForm.bPlanName" placeholder="计划名称" style="width:420px;"></el-input>
                    </el-form-item>
                    <el-row>
                        <el-col :span="8">
                            <el-form-item label="学生：" prop="bSID">
                                <el-select v-model="ruleForm.bSID" placeholder="请选择学生">
                                    <el-option :label="item.SName" :value="item.SID" v-for="item in studyList" :key="item.SID"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="教师：" prop="bTID">
                                <el-select v-model="ruleForm.bTID" placeholder="请选择教师">
                                    <el-option :label="item.T_Name" :value="item.TID" v-for="item in teacherList" :key="item.TID"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    
                    <el-form-item label="计划详情：" prop="bJXYQ">
                        <ele-editor editorId=“teachingEditor” v-model="ruleForm.bJXYQ"></ele-editor>
                    </el-form-item>
                    
                    <el-form-item class="t-r" style="margin-top:10px;">
                        <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </el-card>
    </div>
</template>

<script>
import elewarp from '@/components/warp-slot.vue'
import {addPlanList,editPlanList,lookPlanList} from '@/api/index.js'

import eleEditor from '@/components/editor.vue'
export default {
    name: 'teaching',
    components: {
        elewarp,
        eleEditor
    },
    data() {
        return {
            editorOption:{
                modules:{
                    toolbar:[
                        ["bold", "italic", "underline", "strike"],
                        ["code-block",{list: "ordered"},{list: "bullet"}],
                        [{"color":[]},{"background":[]}],
                        [{indent: "-1"},{indent: "+1"}],
                        [{align: []}],
                        ["link", "image"]
                    ]
                },
                placeholder:'请输入'
            },
            ruleForm:{
                bPlanName:'',
                bJXYQ:'',
                bSID:'',
                bTID:''
            },
            rules:{
                bPlanName:[
                    { required: true, message: '学年不能为空', trigger: 'change' }
                ],
                bJXYQ:[
                    { required: true, message: '计划详情不能为空', trigger: 'blur' }
                ],
                // bSID:[
                //     { required: true, message: '请选择学生', trigger: 'change' }
                // ],
                // bTID:[
                //     { required: true, message: '请选择老师', trigger: 'change' }
                // ],
            },
            week:[]
        }
    },
    computed:{
        rowvalue(){
            return this.week.length + 2
        },
        teacherList() {
            return this.$store.state.allTeacherList;
        },
        studyList() {
            return this.$store.state.allStudyList;
        }
    },
    created() {
        this.syudyid = this.$route.query.id;
        if(this.syudyid) {
            this._getStudyInfo();
        }
    },
    methods:{
        _getStudyInfo(){
            lookPlanList({bTPID:this.syudyid}).then((res) => {
                if(res) {
                    this.ruleForm.bPlanName = res.PlanName;
                    this.ruleForm.bJXYQ = res.JXYQ;
                    this.ruleForm.bSID = res.SID;
                    this.ruleForm.bTID = res.TID;
                }
            })
        },
        submitForm(){
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    let params ={
                        bPlanName:this.ruleForm.bPlanName,
                        bJXYQ:this.ruleForm.bJXYQ,
                        bSID:this.ruleForm.bSID,
                        bTID:this.ruleForm.bTID
                    }
                    if(this.syudyid) {
                        editPlanList({...params,bTPID:this.syudyid}).then((res) => {
                            if(res) {
                                if(res.code == 200) {
                                    this.$util.tipsMsg('success','修改成功！');
                                    this.goback()
                                } else {
                                    this.$util.tipsMsg('error',res.message);
                                }
                            }
                        }).catch((err) => {

                        })
                    } else {
                        addPlanList(params).then((res) => {
                            if(res) {
                                if(res.code == 200) {
                                    this.$util.tipsMsg('success','添加成功！');
                                    this.goback()
                                } else {
                                    this.$util.tipsMsg('error',res.message);
                                }
                            }
                        }).catch((err) => {

                        })
                    }
                } else {
                    return false;
                }
            })
        },
        onReset(){
            this.$refs.searchform.resetFields()
        },
        addPlan(){
            this.week.push({times:this.week.length + 1,value:this.ruleForm.new})
            this.ruleForm.new = "";
        },
        removePlan(index){
            this.$util.openTips.call(this,'是否确定删除？')
            .then((res) => {
                this.week.splice(index,1);
            }).catch((err) => {
                console.log(err)
            })
            
        },
        handleSelectionChange(row){
            console.log(row)
            this.tableSelection = row;
        },
        // 编辑
        handleClickedit(row){
            this.$refs.addstudy.dialogVisible = true;
        },
        // 删除
        handleClicklook(row){
            console.log(row)
            this.$router.push({path:'/studyDetails',query:{id:row.id}})
        },
        // 全选
        goback(){
            this.$router.push({path:'/teaching/teaching-list'})
        },
        // 全反选
        handleClickSelectOther(){
            this.tableData.forEach(row => {
                this.$refs.table.toggleRowSelection(row);
            });
        },
        // 添加学生
        addStudyInfo(){
            this.$refs.addstudy.dialogVisible = true;
        }
    }
}
</script>
<style lang="less">
.plan-list{
    th{
        background-color: #F5F7FA;
        line-height: 40px;
    }
    th,td{
        border:1px solid #ccc;
    }
    td .el-form-item{
        margin-bottom: 0px;
    }
    .week{
        width: 100px;
        .el-icon-close:hover{
            color:red;
        }
    }
    .no-border{
        border-bottom: 0px;
    }
    .btn-tr{
        td{
            border-top:0px;
        }
    }
    .ql-editor{
        min-height: 400px !important;
    }
}
.teaching{
    .el-card__body{
        // height:calc(100% - 70px);
    }
    .box-card-table{
        overflow-y: auto;
        box-sizing: border-box;
    }
    .ql-editor{
        min-height:300px;
    }
    
}
</style>
